package com.small.proxy;

import java.lang.reflect.Method;
import java.util.List;
import com.small.aop.AopCache;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * 为切面生成代理对象 
 * @author 乐乐
 *
 */
public class ProxyFactor {
  
  /**
   * 匿名内部类方式使用字节码生成器代理生成class文件
   * @param <T>
   * @param targetClass  目标类
   * @param proxies      目标方法的代理链对象
   * @return
   */
  @SuppressWarnings("unchecked")
  public static <T> T createProxy(final Class<?> targetClass) {
    
    return (T) Enhancer.create(targetClass, new MethodInterceptor() {
      
      public Object intercept(Object obj, Method method, Object[] param, MethodProxy proxy) throws Throwable {
        List<Proxy> proxies = AopCache.getCache(targetClass).get(method);
        
        return new ProxyChain(targetClass, obj, param, method, proxy, proxies).doChain();
      }
    });
    
  }
  
  
}
